<template>
  <div class="solver_problem_solve">
    <!--问题转签-->
    <problem-transfer ref="ProblemTransfer" @submitSuccess="init"></problem-transfer>
    <!--处理意见-单件-->
    <problem-conclude ref="ProblemConclude" @submitSuccess="init"></problem-conclude>
    <!--处理意见-批次-->
    <batch-conclude ref="BatchConclude" @submitSuccess="init"></batch-conclude>
    <!--处理明细-->
    <processing-detail ref="ProcessingDetail"></processing-detail>
    <!--操作记录-->
    <log-detail ref="logDetail"></log-detail>
    <one-title title="问题处理工作台" @refreshData="refresh">
      <div class="mes_btn">
        <el-tooltip
          effect="dark"
          :content="unqualifiedBtnStatus.content"
          :disabled="!unqualifiedBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'unqualified'"
            @click="handleUnqualified" :class="{'is-disabled':unqualifiedBtnStatus.status}">
            <i class="iconfont icon-faqibuhegeshenqing"></i>
            发起不合格申请
          </el-button>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          :content="transferBtnStatus.content"
          :disabled="!transferBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'transfer'"
            type="primary" @click="handleTransfer" :class="{'is-disabled':transferBtnStatus.status}">
            <i class="iconfont icon-zhuanqian"></i>
            问题转签
          </el-button>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          :content="ConclusionBtnStatus.content"
          :disabled="!ConclusionBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'conclusion'"
            type="success" @click="handleConclusion" :class="{'is-disabled':ConclusionBtnStatus.status}">
            <i class="iconfont icon-btn_Save"></i>
            处理意见
          </el-button>
        </el-tooltip>
      </div>
    </one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        :searchList="tableColConfig"
        ref="mesSearch"
        :searchResult="searchCount"
        @searchInfo="_info">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          :data="listData.list"
          ref="tableNode"
          v-loading="isLoading"
          :max-height="tableMaxHeight"
          :row-class-name="({row}) => !selectItems.some((item) => item.id === row.id) || 'line_selected'"
          @row-click="row => $refs.tableNode.toggleRowSelection(row)"
          @sort-change="handleSortChange"
          @selection-change="handleSelectionChange"
          @filter-change="handleFilterChange"
          border
          stripe
          tooltip-effect="dark">
          <el-table-column
            width="40"
            type="selection"
            align="center">
          </el-table-column>
          <mes-col-comp
            :key="index"
            v-for="(column, index) in columnList"
            :column="column">
          </mes-col-comp>
          <el-table-column
            label="操作"
            show-overflow-tooltip
            :resizable="false"
            align="center"
            fixed="right"
            min-width="160">
            <template slot-scope="scope">
              <div class="operation">
                <span class="jump_item" @click="handleProcessingDetail(scope.row)">处理明细</span>
                <span class="jump_item" @click="handleViewLog(scope.row)">操作记录</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
    </div>
  </div>
</template>

<script>
import tableColumns from './tableColumns'
import {BatchConclude, ProblemConclude, ProblemTransfer} from './components'
import ProcessingDetail from './components/ProcessingDetail'

export default {
  name: 'SolverProblemSolve',
  components: {
    ProblemTransfer,
    ProblemConclude,
    BatchConclude,
    ProcessingDetail,
  },
  data() {
    return {
      tableColConfig: tableColumns, // 列配置
      isLoading: false, // 数据加载中
      searchData: {}, // 查询参数
      sortData: {}, // 排序对象
      searchCount: NaN, // 搜索结果数
      selectItems: [], // 选中的数据,
      concludeDetail: [] // 结论详细信息
    }
  },
  mounted() {
    this.init()
  },
  computed: {
    // 列表数据
    listData() {
      let getList = this.$store.state.problemProcess.concludeListData
      if (getList.list) {
        getList.list.map(item => {
          this.$set(item, 'inputQuantity', item.inputQuantity || item.turnoverProductionQuantity)
        })
      }
      return getList
    },
    // 任务日志
    logList() {
      return this.$store.state.problemProcess.logList
    },
    // 问题转签按钮
    transferBtnStatus() {
      let status = false
      if (this.selectItems.length > 1) {
        status = true
        return {
          status: status,
          content: '请选择一条数据'
        }
      }
      if (this.selectItems.some((item) => item.problemProcessStatusValue !== 'Unprocessed')) {
        status = true
      }
      return {
        status: status,
        content: '请选择处理状态为"待处理"的数据'
      }
    },
    // 处理结论按钮
    ConclusionBtnStatus() {
      let status = false
      if (this.selectItems.length > 1) {
        status = true
        return {
          status: status,
          content: '请选择一条数据'
        }
      }
      if (this.selectItems.some((item) => item.problemProcessStatusValue !== 'Unprocessed')) {
        status = true
      }
      return {
        status: status,
        content: '请选择处理状态为"待处理"的数据'
      }
    },
    // 发起不合格申请按钮
    unqualifiedBtnStatus() {
      let status = false
      if (this.selectItems.length > 1) {
        status = true
        return {
          status: status,
          content: '请选择一条数据'
        }
      }
      if (this.selectItems.some((item) =>
        (item && item.qmsProcessStatusValue === 'Processing') || (item && item.qmsProcessStatusValue === 'Finished'))) {
        console.log(1)
        status = true
      }
      return {
        status: status,
        content: '请选择审理状态为空的数据'
      }
    },
    // 列循环
    columnList() {
      return this.tableColConfig.filter(item => item.noForCol !== true && item.isShow !== false)
    }
  },
  methods: {
    // 列筛选回调
    handleFilterChange(filters) {
      let filterParams = this.$filterParams(this.tableColConfig, filters)
      Object.assign(this.searchData, filterParams.search)
      this.sortData = filterParams.order // 排序对象
      this.$refs.tableNode.clearSort()
      this.init()
    },
    // 初始化方法
    init(page) {
      this.isLoading = true
      let postData = {
        method: 'post',
        url: 'a/workstageproblemprocess/workstageProblemProcess/list',
        module: 'problemProcess',
        data: {
          pageNo: page ? page.pageNo : 1,
          pageSize: page ? page.pageSize : this.listData.pageSize || 15,
          sort: {
            problemProcessStatusValue: 'desc'
          }
        },
        state: 'concludeListData'
      }
      if (Object.keys(this.searchData).length > 0) {
        postData.data.query = this.searchData
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.data.sort = this.sortData
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 查询
    _info(data) {
      this.searchData = data
      this.$refs.tableNode.clearSort()
      this.sortData = {}
      this.$refs.tableNode.clearFilter()
      this.init()
    },
    // 排序方法
    handleSortChange(e) {
      this.sortData = this.$getSortOrder(e)
      this.init()
    },
    // 刷新方法
    refresh() {
      this.searchData = {} // 查询参数
      this.sortData = {} // 排序对象
      this.$refs.mesSearch.clean()
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.selectItems = []
      this.init()
    },
    // 选中方法
    handleSelectionChange(val) {
      this.selectItems = val
    },
    // 操作记录
    handleViewLog(row) {
      this.$refs.logDetail.openDialog({
        title: '周转码',
        name: row.turnoverNumber,
        url: 'a/turnoverstatuschangehistory/turnoverStatusChangeHistory/list',
        query: {
          turnoverBatchId: row.turnoverId
        }
      })
    },
    // 处理明细
    handleProcessingDetail(row) {
      this.$refs.ProcessingDetail.openDialog(row)
    },
    // 问题转签
    handleTransfer() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条数据')
      }
      if (this.transferBtnStatus.status) return
      this.$refs.ProblemTransfer.openDialog(this.selectItems)
    },
    // 处理意见
    handleConclusion() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条数据')
      }
      if (this.ConclusionBtnStatus.status) return
      if (this.selectItems[0].trackDemandId === 'ID_TD_Single') {
        this.$refs.ProblemConclude.openDialog(this.selectItems)
      }
      if (this.selectItems[0].trackDemandId === 'ID_TD_Batch') {
        this.$refs.BatchConclude.openDialog(this.selectItems)
      }
    },
    // 不合格申请单
    handleUnqualified() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条数据')
      }
      if (this.unqualifiedBtnStatus.status) return
      this.mesConfirm('选中的记录将进行不合格申请单操作, 是否继续?', '不合格申请单提示').then(() => {
        this.$store.dispatch('saveData', {
          method: 'post',
          url: 'a/workstageproblemprocess/workstageProblemProcess/qmsprocess',
          data: {
            problemId: this.selectItems[0].id,
            viewSource: 1
          },
          noCommit: true
        }).then(() => {
          this.tipMessage('success', '提交成功')
          this.init()
        })
      })
    },
    /**
     * 提示
     * @param str 提示内容
     */
    tipMessage(type, str) {
      this.$message({
        type: type,
        message: str,
        duration: 1500,
        center: true
      })
    }
  }
}
</script>
<style lang="less" scoped>
  .solver_problem_solve {
    .viewLog {
      color: #0066cc;
      cursor: pointer;
    }

    .operation {
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
  }
</style>
